package com.ruoyi.web.controller.xxc;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.news.domain.XcxNews;
import com.ruoyi.news.domain.XcxOrderMain;
import com.ruoyi.news.domain.XcxWalletInfo;
import com.ruoyi.news.domain.XxcWallet;
import com.ruoyi.news.service.IXcxOrderMainService;
import com.ruoyi.news.service.IXcxWalletInfoService;
import com.ruoyi.news.service.IXxcWalletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单主Controller
 * 
 * @author ruoyi
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/xxc/orderMain")
public class XcxOrderMainController extends BaseController
{
    @Autowired
    private IXcxOrderMainService xcxOrderMainService;

    @Autowired
    public IXxcWalletService xxcWalletService;

    @Autowired
    private IXcxWalletInfoService xcxWalletInfoService;

    /**
     * 查询订单主列表
     */
    //@PreAuthorize("@ss.hasPermi('system:main:list')")
    @GetMapping("/list")
    public TableDataInfo list(XcxOrderMain xcxOrderMain)
    {
        startPage();
        List<XcxOrderMain> list = xcxOrderMainService.selectXcxOrderMainList(xcxOrderMain);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:main:list')")
    @GetMapping("/listMyOrder")
    public TableDataInfo listMyOrder(XcxOrderMain xcxOrderMain)
    {
        startPage();
        List<XcxOrderMain> list = xcxOrderMainService.selectListMyOrder(xcxOrderMain);
        return getDataTable(list);
    }

    /**
     * 导出订单主列表
     */
    @PreAuthorize("@ss.hasPermi('system:main:export')")
    @Log(title = "订单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XcxOrderMain xcxOrderMain)
    {

        List<XcxOrderMain> list = xcxOrderMainService.selectXcxOrderMainList(xcxOrderMain);
        ExcelUtil<XcxOrderMain> util = new ExcelUtil<XcxOrderMain>(XcxOrderMain.class);
        util.exportExcel(response, list, "订单主数据");
    }

    @GetMapping(value = "/readNum/{id}")
    public AjaxResult readNum(@PathVariable("id") String id)
    {
        XcxOrderMain xcxOrderMain = xcxOrderMainService.selectXcxOrderMainById(id);
        if(xcxOrderMain.getReadNum() == null){
            xcxOrderMain.setReadNum(1);

        }else {
            Integer read = xcxOrderMain.getReadNum() + 1;
            xcxOrderMain.setReadNum( read );
        }
        int flag =  xcxOrderMainService.updateXcxOrderMain(xcxOrderMain);
        if(flag >0)
            return success(xcxOrderMain);
        else
            return error("系统异常");
    }


    /**
     * 获取订单主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(xcxOrderMainService.selectXcxOrderMainById(id));
    }

    /**
     * 新增订单主
     */
    @PreAuthorize("@ss.hasPermi('system:main:add')")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {


        LoginUser loginUser =  (LoginUser)user;
        SysUser sysUser = loginUser.getUser();
        XxcWallet meXxcWallet =  xxcWalletService.selectXxcWalletByUserId(sysUser.getUserId()+"");

        BigDecimal nowPrice = new BigDecimal(xcxOrderMain.getPrice());
        if(meXxcWallet.getYuE().compareTo(nowPrice) >-1) {
            XcxOrderMain free = xcxOrderMainService.insertXcxOrderMain(xcxOrderMain, sysUser);

            if(free != null) {

                XcxWalletInfo xcxWalletInfo = new XcxWalletInfo();
                xcxWalletInfo.setOrderMain(xcxOrderMain);
                xcxWalletInfo.setId(IdUtils.fastSimpleUUID());
                xcxWalletInfo.setUserId(sysUser.getUserId()+"");
                xcxWalletInfo.setType("2");
                xcxWalletInfo.setBid(free.getOrderId());
                xcxWalletInfo.setOrderMain( xcxOrderMain );
                return success(xcxWalletInfoService.insertXcxWalletInfo(xcxWalletInfo,sysUser));
            }else {
                return error("系统异常,请联系客服");
            }
        }else{
            return error("余额不足！");
        }
    }

    /**
     * 修改订单主
     */
    @PreAuthorize("@ss.hasPermi('system:main:edit')")
    @Log(title = "订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XcxOrderMain xcxOrderMain)
    {
        return toAjax(xcxOrderMainService.updateXcxOrderMain(xcxOrderMain));
    }

    /**
     * 删除订单主
     */
    @PreAuthorize("@ss.hasPermi('system:main:remove')")
    @Log(title = "订单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(xcxOrderMainService.deleteXcxOrderMainByIds(ids));
    }

}
